package com.swm.julie.rest.sausage.service;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.swm.julie.rest.sausage.model.Product;

public class XMLRPCSearchProdutImpl {

	static Logger logger = LoggerFactory.getLogger(SearchProduct.class);
	
	static String sausageRPCServerURL = "http://localhost:8888/SausageRPCServer/xmlrpc";
//	static String sausageRPCServerURL = "http://210.118.74.202:8888/SausageRPCServer/xmlrpc";
	
//	@Test
	public void search() throws MalformedURLException, XmlRpcException
	{
		standardSearch("청담");
		itemSearch("fashion", "패딩");
		companySearch("coupang");
		itemCompanySearch("coupang", "엔제리너스");
		provinceSearch("Seoul");
	}

	static public ArrayList<Product> standardSearch(String query) throws MalformedURLException, XmlRpcException 
	{
		// TODO Auto-generated method stub
//		System.out.println("XMLRPCSearchImpl.standardSearch()");
		if(query == null) return null;
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		
		Object[] rest = (Object[]) client.execute("SausageSearcher.standardSearch", params);
		
		ArrayList<Product> res = new ArrayList<Product>();
		
		for(Object object : (Object[]) rest)
		{
			Product product = (Product) object;
			res.add(product);
//			System.out.println("result : " + product.getItemName());
		}
		
		return res;
	}
	
	static public ArrayList<Product> itemSearch(String item, String query) throws MalformedURLException, XmlRpcException
	{
//		System.out.println("XMLRPCSearchImpl.itemSearch()");
		ArrayList<Product> res = new ArrayList<Product>();
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{item, query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.itemSearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			res.add(product);
			
//			System.out.println("result : " + product.getItemName());
		}
		
		return res;
	}
	
	static public ArrayList<Product> companySearch(String query) throws MalformedURLException, XmlRpcException 
	{
//		System.out.println("XMLRPCSearchImpl.companySearch()");
		ArrayList<Product> res = new ArrayList<Product>();
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.companySearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			res.add(product);
		}
		
		return res;
	}
	
	
	static public ArrayList<Product> itemCompanySearch(String company, String query) throws MalformedURLException, XmlRpcException 
	{
//		System.out.println("XMLRPCSearchImpl.itemCompanySearch()");
		ArrayList<Product> res = new ArrayList<Product>();
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{company, query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.itemCompanySearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			res.add(product);
		}
		
		return res;
	}
	
	public static ArrayList<Product> provinceSearch(String query) throws MalformedURLException ,XmlRpcException 
	{
//		System.out.println("XMLRPCSearchImpl.provinceSearch()");
		ArrayList<Product> res = new ArrayList<Product>();
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.provinceSearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			res.add(product);
//			System.out.println("result : " + product.getItemName());
		}
		
		return res;
	}
	
	public static ArrayList<Product> itemPovinceSearch(String province, String query) throws MalformedURLException ,XmlRpcException 
	{
		logger.info("item province : " + province + ", " + query);
		ArrayList<Product> res = new ArrayList<Product>();
		
		XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
		config.setServerURL(new URL(sausageRPCServerURL));
		config.setEnabledForExtensions(true);
		
		XmlRpcClient client = new XmlRpcClient();
		client.setConfig(config);
		
		Object[] params = new Object[]{province, query};
		Object[] rest = (Object[]) client.execute("SausageSearcher.itemProvinceSearch", params);
		
		for(Object object : rest)
		{
			Product product = (Product) object;
			res.add(product);
		}
		
		return res;
	}
}
